package com.situ.mall.controller;

import com.situ.mall.pojo.User;
import com.situ.mall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/add")
    public String add(User user) {
        userService.add(user);
        //重定向，让浏览器重新发一个请求，查找最新的数据
        return "redirect:/user/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "user_add";
    }

    @RequestMapping("/list")
    public String list(Model model) {
        List<User> list = userService.list();
        //把数据放到Model里面
        model.addAttribute("list", list);
        //转发到user_list.jsp页面进行展示
        return "user_list";
    }

    // /user/list
    @RequestMapping("/list1")
    @ResponseBody  //以json形式返回数据
    public List<User> list1() {
        List<User> list = userService.list();

        return list;
    }

}
